﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="RefreshPoolMembersCommand.cs" company="CET">
//   The Center of Educational Technology
// </copyright>
// <summary>
//   Defines the LoadPoolsCommand type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace F5.BigIp.LocalTrafficManager.UI.Commands
{
    using System.ComponentModel;
    using System.Linq;

    using F5.BigIp.LocalTrafficManager.Interfaces;
    using F5.BigIp.LocalTrafficManager.UI.ViewModels;

    /// <summary>
    /// The load pools command.
    /// </summary>
    public class RefreshPoolMembers : BackgroundCommand
    {
        private IPoolMemberService PoolMemberService { get; set; }

        /// <summary>
        /// The view model.
        /// </summary>
        private readonly MainViewModel viewModel;

        /// <summary>The pool.</summary>
        private IPool pool;

        /// <summary>
        /// Initializes a new instance of the <see cref="RefreshPoolMembers"/> class. 
        /// </summary>
        /// <param name="viewModel">
        /// The view model.
        /// </param>
        /// <param name="poolMemberService"></param>
        public RefreshPoolMembers(MainViewModel viewModel, IPoolMemberService poolMemberService)
        {
            this.PoolMemberService = poolMemberService;
            this.viewModel = viewModel;
        }

        /// <summary>
        /// The can execute.
        /// </summary>
        /// <param name="parameter">
        /// The parameter.
        /// </param>
        /// <returns>
        /// The <see cref="bool"/>.
        /// </returns>
        public override bool CanExecute(object parameter)
        {
            if (!base.CanExecute(parameter))
            {
                return false;
            }

            this.pool = parameter as IPool;

            return this.pool != null && 
                this.viewModel != null && 
                this.PoolMemberService != null;
        }

        /// <summary>
        /// The do work.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        protected override void DoWork(object sender, DoWorkEventArgs e)
        {
            this.viewModel.IsBusy = true;

            var updates = this.PoolMemberService.GetPoolMembers(this.pool);

            foreach (var update in updates)
            {
                var poolMember = this.viewModel.PoolMembers.FirstOrDefault(k => k.Address == update.Address);

                if (poolMember != null)
                {
                    poolMember.Status = update.Status;
                    poolMember.Statistics = update.Statistics;
                    poolMember.LastUpdate = update.LastUpdate;
                }
                else
                {
                    this.viewModel.PoolMembers.Add(update);
                }
            }
        }

        /// <summary>
        /// The run worker completed.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        protected override void RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            this.viewModel.IsBusy = false;
        }
    }
}
